package Selenium;

import Utils.CommonValue;
import Utils.ReadTxt;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * created by zhangyuhang on 2018/7/13
 */
public class PicSelenium {

    public WebDriver driver;

    public void InitDriver() {
        driver = new ChromeDriver();
        driver.get(CommonValue.pic_login_url);
        driver.manage().window().maximize();
    }

    public void login(){
        driver.findElement(By.id("login-username")).sendKeys(CommonValue.pic_login_id_chen);
        driver.findElement(By.id("login-password")).sendKeys(CommonValue.pic_login_pass_chen);
        driver.findElement(By.className("btn-form-submit")).click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public void loginByQQ(){
        driver.findElement(By.xpath("//*[@id=\"wrapper\"]/div[2]/div[2]/div/div/div/div/div[2]/form/div[2]/div/div/a"));

    }



    public void downloadByUrl(String url,List<String> list){
        driver.get(url);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //查看下面收藏栏是否存在，存在就关闭他
        try{
            WebElement close = driver.findElement(By.xpath("//*[@id=\"likebox-footer\"]/div/div[1]/div[3]/ul/li[4]"));
            if (close !=null){
                close.click();
            }
        }catch (Exception e){

        }

        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();

        driver.findElement(By.xpath("//*[@id=\"popup-image-download\"]/div[3]/a")).click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        System.out.println("==================================");
        System.out.println(url+" has been download");
        list.add(url);
        System.out.println(list);
        System.out.println("==================================");

    }

    public void download(String id,List<String> list){
        driver.get(CommonValue.pic_find_url+id);
        driver.findElement(By.className("uitooltip")).click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //查看下面收藏栏是否存在，存在就关闭他
        try{
            WebElement close = driver.findElement(By.xpath("//*[@id=\"likebox-footer\"]/div/div[1]/div[3]/ul/li[4]"));
            if (close !=null){
                close.click();
            }
        }catch (Exception e){

        }

        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();

        driver.findElement(By.xpath("//*[@id=\"popup-image-download\"]/div[3]/a")).click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        System.out.println("==================================");
        System.out.println(id+" has been download");
        list.add(id);
        System.out.println(list);
        System.out.println("==================================");

    }


    public static void main(String args[]) throws IOException {

        downloadByUrl();
    }

    //根据图片id下载
    public static void downloadById(){
        String[] picIds = {"36613252","98198132","77497181"};
        List<String> hasBeenDownload = new ArrayList<String>();
        PicSelenium picSelenium = new PicSelenium();
        picSelenium.InitDriver();
        picSelenium.login();
        for (String picId:picIds) {
            picSelenium.download(picId, hasBeenDownload);
        }
    }


    //根据图片url下载
    public static void downloadByUrl() throws IOException {
        List<String> list = ReadTxt.read("/Users/zhangyuhang/Desktop/wk/youzantest/seleniumtest/src/main/resources/picid.txt");

        List<String> hasBeenDownload = new ArrayList<String>();
        PicSelenium picSelenium = new PicSelenium();
        picSelenium.InitDriver();
        picSelenium.login();
        for (String url :list){
            picSelenium.downloadByUrl(url, hasBeenDownload);
        }


    }

}
